package com.gc.ykqx.service.util;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;

import static com.gc.ykqx.service.util.App.getContext;

/**
 * Created by wsd on 2017/10/13.
 */

public class LocationManager implements AMapLocationListener, GeocodeSearch.OnGeocodeSearchListener {
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    private LocationListener listener;
    private String address = "永康";

    public LocationManager(LocationListener listener) {
        this.listener = listener;
    }

    public void setmLocationOption() {
        //声明mLocationOption对象
        mLocationClient = new AMapLocationClient(App.getContext());
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位监听
        mLocationClient.setLocationListener(LocationManager.this);
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        //   mLocationOption.setInterval(2000 * 30 * 5);
        //设置定位参数
        mLocationOption.setOnceLocation(true);
        mLocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
        // 注意设置合适的定位时间的间隔（最小间隔支持为1000ms），并且在合适时间调用stopLocation()方法来取消定位请求
        // 在定位结束后，在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下，定位无论成功与否，都无需调用stopLocation()方法移除请求，定位sdk内部会移除
        //启动定位
        mLocationClient.startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                double latitude = amapLocation.getLatitude();//获取纬度
                double longitude = amapLocation.getLongitude();//获取经度
                if (longitude > 120.010381 && longitude < 120.075634 && latitude > 28.876353 && latitude < 28.924734) {
                    address = "永康市区";
                } else {
                    getAddress(latitude, longitude);//定位成功
                    return;
                }
            }
        }
        retAddress();
    }

    private void retAddress() {
        if (listener != null) {
            listener.onLocationSuccess(address);
        }
    }

    private void getAddress(double latitude, double longitude) {
        GeocodeSearch gs = new GeocodeSearch(getContext());
        gs.setOnGeocodeSearchListener(this);
        LatLonPoint point = new LatLonPoint(latitude, longitude);
        RegeocodeQuery query = new RegeocodeQuery(point, 200, GeocodeSearch.AMAP);
        gs.getFromLocationAsyn(query);
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        if (i == 1000) {
            RegeocodeAddress ra = regeocodeResult.getRegeocodeAddress();
            if (ra != null) {
                String district = ra.getDistrict();//市名
                if (district.equals("永康市")) {
                    String township = ra.getTownship();//镇名
                    if (township != null && township.length() >= 2) {
                        address = township.substring(0, 2);
                    }
                }
            }
        }
        retAddress();
    }

    public void destroy() {
        if (mLocationClient != null) {
            mLocationClient.onDestroy();
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
    }

    public interface LocationListener {
        //定位成功返回乡镇信息
        void onLocationSuccess(String address);
    }
}
